package controller;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {

	public static String fomatDomain(String str) {
		if (str != null)
			str = str.toLowerCase().replaceAll("(http|https)+://(www.)*", "")
					.replaceAll("^[\\w\\W]+[\\/]{2}", "")
					.replaceAll("[\\/]+[\\w\\W]*$", "").replaceAll("www.", "")
					.replaceAll("[\\?]+[\\w\\W]*$", "")
					.replaceAll("[ !@#$%^&*?()\\[\\]{}+=,:;|\\\'\\\"/\\\\`~]+", "")
					.trim();

		return str;
	}

	public static String textCrop(String content, String begin, String end) {
		int start = content.indexOf(begin) + begin.length();

		int finish = content.indexOf(end, start);
		return content.substring(start, finish);
	}

	public static String md5(String str) throws NoSuchAlgorithmException {
		MessageDigest md;

		md = MessageDigest.getInstance("MD5");
		md.update(str.getBytes());

		byte byteData[] = md.digest();

		// convert the byte to hex format method 1
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < byteData.length; i++) {
			sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16)
					.substring(1));
		}

		str = sb.toString();
		return str;
	}

	public static String returnWhois(String str)
			throws NoSuchAlgorithmException {
		Pattern p = Pattern.compile("[^ :]+@[^ ]+");

		Matcher m = p.matcher(str.replaceAll("\\<.*?\\>", " "));
		String email = "";
		while (m.find()) {
			email = m.group().toLowerCase().trim().replaceAll("\\(", "")
					.replaceAll("\\)", "");
			str = str
					.replaceAll(
							email,
							"<img src='http://source.domaintools.com/email.pgif?md5="
									+ md5(email)
									+ "&face=arial&size=9&color=000000&bgcolor=FFFFFF&face=arial&size=9&color=0000FF&bgcolor=FFFFFF&format[]=underline&format[]=transparent&format[]=transparent'/>");
		}
		return str;
	}

	public static Boolean checkDomain(String domain) {
		ResourceBundle resource = ResourceBundle.getBundle("language",
				new Locale("en"));
		String re_domain = domain;

		String[] listDomain = resource.getString("domain").split(" ");
		for (int i = 0; i < listDomain.length; i++) {
			re_domain = re_domain
					.replaceAll("[.]{1}" + listDomain[i] + "$", "");
		}

		if (re_domain.length() != domain.length()) {
			return true;
		}

		return false;
	}

	public static String returnString(String content, String link) {

		String[] sbc = content.split(": 1");
		String str = "";
		for (int i = 0; i < sbc.length; i++) {
			if (!sbc[i].replaceAll("[,{}\\\"]+", "").trim().equals("")) {
				if (!link.equals("")) {
					str += "<a href='/"
							+ link
							+ "/"
							+ sbc[i].replaceAll("[,{}\\\"]+", "").trim()
									.replaceAll("[&]{3}", ".")
							+ "' rel=\"nofollow\"> "
							+ sbc[i].replaceAll("[,{}\\\"]+", "").trim()
									.replaceAll("[&]{3}", ".") + " </a>"
							+ " | ";
				} else {
					str += sbc[i].replaceAll("[,{}\\\"]+", "").trim()
							.replaceAll("[&]{3}", ".")
							+ " | ";
				}
			}
		}
		return str;
	}

	public static String returnText(String content) {

		String[] sbc = content.split(": 1");
		String str = "";
		for (int i = 0; i < sbc.length; i++) {
			if (!sbc[i].replaceAll("[,{}\\\"]+", "").trim().equals("")) {
				str += sbc[i].replaceAll("[,{}\\\"]+", "").trim()
						.replaceAll("[&]{3}", ".")
						+ ",";
			}
		}
		return str;
	}

	public static int returnLength(String content) {

		String[] sbc = content.split(": 1");
		int str = 0;
		for (int i = 0; i < sbc.length; i++) {
			if (!sbc[i].replaceAll("[,{}\\\"]+", "").trim().equals("")) {
				str += sbc[i].replaceAll("[,{}\\\"]+", "").trim()
						.replaceAll("[&]{3}", ".").length();
			}
		}
		return str;
	}

	public static int returnNumber(String content) {

		String[] sbc = content.split(": 1");
		int str = 0;
		for (int i = 0; i < sbc.length; i++) {
			if (!sbc[i].replaceAll("[,{}\\\"]+", "").trim().equals("")) {
				str += 1;
			}
		}
		return str;
	}

	public static String convertToString(List list) {

		String str = "";
		for (Object jString : list) {
			str += jString.toString() + ",";

		}
		return str;
	}

	public static int checkLengthDomain(String domain) {
		ResourceBundle resource = ResourceBundle.getBundle("language",
				new Locale("en"));
		String re_domain = domain;
		re_domain = re_domain.replaceAll("com[.]{1}[a-zA-Z]+$", "");

		String[] check = resource.getString("domain").split(" ");
		for (int i = 0; i < check.length; i++) {
			re_domain = re_domain.replaceAll("[.]+"+check[i]+"$", "");
		}
		
		re_domain = re_domain.replaceAll("[.]+$", "");
	
		return re_domain.length();
	}

	public static String returnDomain(String domain) {
		String re_domain = domain;
		re_domain = re_domain.replaceAll("com[.]{1}[a-zA-Z]+$", "");
		ResourceBundle resource = ResourceBundle.getBundle("language",
				new Locale("en"));
		String[] check = resource.getString("domain").split(" ");
		for (int i = 0; i < check.length; i++) {
			re_domain = re_domain.replaceAll("[.]+"+check[i]+"$", "");
		}
		
		re_domain = re_domain.replaceAll("[.]+$", "");

		String str = re_domain.replaceAll("[.]{1}[^.]+$", "");
		
		if (re_domain.length() != str.length()) {
			
			return re_domain.replaceAll(str+"[.]{1}", "");
		} else {
			return re_domain;
		}
	}
	
	public static Boolean checkSubDomain(String domain)
	{
		Boolean checks = false;
		
		String re_domain = domain;
		re_domain = re_domain.replaceAll("com[.]{1}[a-zA-Z]+$", "");
		ResourceBundle resource = ResourceBundle.getBundle("language",
				new Locale("en"));
		String[] check = resource.getString("domain").split(" ");
		for (int i = 0; i < check.length; i++) {
			re_domain = re_domain.replaceAll("[.]+"+check[i]+"$", "");
		}
		
		re_domain = re_domain.replaceAll("[.]+$", "");

		String str = re_domain.replaceAll("[.]{1}[^.]+$", "");
		
		if (re_domain.length() != str.length()) {
			
			checks = true;
		}
		
		return checks;
	}
	
	public static String returnEndDomain(String domain) {
		String re_domain = domain;
		re_domain = re_domain.replaceAll("com[.]{1}[a-zA-Z]+$", "");
		ResourceBundle resource = ResourceBundle.getBundle("language",
				new Locale("en"));
		String[] check = resource.getString("domain").split(" ");
		for (int i = 0; i < check.length; i++) {
			re_domain = re_domain.replaceAll("[.]+"+check[i]+"$", "");
		}
		
		re_domain = re_domain.replaceAll("[.]+$", "");

		if (re_domain.length() != domain.length()) {
			return re_domain.replaceAll("[.]+$", "").replaceAll(
					"^[a-zA-Z_\\-0-9]+[.]{1}", "");
		} else {
			return re_domain.replaceAll("[.]{1}[a-zA-Z]+$", "").replaceAll(
					"^[a-zA-Z_\\-0-9]+[.]{1}", "");
		}
	}

	public static String returnSubDomain(String domain) {
		String re_domain = domain;
		re_domain = re_domain.replaceAll("com[.]{1}[a-zA-Z]+$", "");
		ResourceBundle resource = ResourceBundle.getBundle("language",
				new Locale("en"));
		String[] check = resource.getString("domain").split(" ");
		for (int i = 0; i < check.length; i++) {
			re_domain = re_domain.replaceAll("[.]+"+check[i]+"$", "");
		}
		
		re_domain = re_domain.replaceAll("[.]+$", "");
		
		String str = re_domain.replaceAll("[.]{1}[^.]+$", "");
		
		if (re_domain.length() != str.length()) {
			
			return " is sub domain of <strong>"
					+ domain.replaceAll(str+"[.]{1}", "")
					+ "</strong>";
		} else {
				return " is top level domain";
		}
	}

	public static String returnSubDomainTo(String domain) {
		String re_domain = domain;
		re_domain = re_domain.replaceAll("com[.]{1}[a-zA-Z]+$", "");
		ResourceBundle resource = ResourceBundle.getBundle("language",
				new Locale("en"));
		String[] check = resource.getString("domain").split(" ");
		for (int i = 0; i < check.length; i++) {
			re_domain = re_domain.replaceAll("[.]+"+check[i]+"$", "");
		}
		
		re_domain = re_domain.replaceAll("[.]+$", "");

		String str = re_domain.replaceAll("[.]{1}[^.]+$", "");
		
		if (re_domain.length() != str.length()) {
			
			return domain.replaceAll(str+"[.]{1}", "");
		} else {
			return domain;
		}
	}

	public static String cutString(String str, int count) {
		String string = str;
		if (str.length() > count) {
			string = str.substring(0, count);
		}
		return string;
	}

}
